/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "gpio.h"
#include "fsmc.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "../TFTLCD/TFTLCD.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t key0_press = 0;
uint8_t key0_press_past = 0;
uint8_t key1_press = 0;
uint8_t key1_press_past = 0;

uint8_t key2_press = 0;
uint8_t key2_press_past = 0;
uint8_t WK_press = 0;
uint8_t WK_press_past = 0;

#define P_MCP_CS	 GPIO_PIN_9
#define P_MCP_CLK	 GPIO_PIN_10
#define P_MCP_DAT	 GPIO_PIN_11

#define MCP41xx_SPI_CLK_H()     HAL_GPIO_WritePin(GPIOG, P_MCP_CLK, GPIO_PIN_SET);
#define MCP41xx_SPI_CLK_L()     HAL_GPIO_WritePin(GPIOG, P_MCP_CLK, GPIO_PIN_RESET);

#define MCP41xx_SPI_DAT_H()     HAL_GPIO_WritePin(GPIOG, P_MCP_DAT, GPIO_PIN_SET);
#define MCP41xx_SPI_DAT_L()     HAL_GPIO_WritePin(GPIOG, P_MCP_DAT, GPIO_PIN_RESET);

#define MCP41xx_SPI_CS_H()      HAL_GPIO_WritePin(GPIOG, P_MCP_CS, GPIO_PIN_SET);
#define MCP41xx_SPI_CS_L()      HAL_GPIO_WritePin(GPIOG, P_MCP_CS, GPIO_PIN_RESET);

void mcp_delay(unsigned int n)
{
    n=n*110;
    while(n--);
}

void MCP41xx_writedata(unsigned char dat1)		//调整数字电位器
{
    unsigned char i,command=0x11;

    MCP41xx_SPI_CS_H();//cs=1;
    MCP41xx_SPI_CLK_L();//sck=1;
    MCP41xx_SPI_CS_L();//cs=0;

    mcp_delay(10);

    for(i=0;i<8;i++)          //写命令
    {
        if(command & 0x80)
        {
            MCP41xx_SPI_DAT_H();//si=1;
        }
        else
        {
            MCP41xx_SPI_DAT_L();//si=0;
        }
        mcp_delay(10);
        MCP41xx_SPI_CLK_L();//sck=1;//sck=1;
        mcp_delay(10);
        MCP41xx_SPI_CLK_H();//sck=0;sck=0;
        mcp_delay(10);

        command=command<<1;
    }


    for(i=0;i<8;i++)          //写数据
    {

        if(dat1 & 0x80)
        {
            MCP41xx_SPI_DAT_H();//si=1;
        }
        else
        {
            MCP41xx_SPI_DAT_L();//si=0;
        }
        mcp_delay(10);
        MCP41xx_SPI_CLK_L();//sck=1;//sck=1;
        mcp_delay(10);
        MCP41xx_SPI_CLK_H();//sck=0;sck=0;
        mcp_delay(10);
        dat1=dat1<<1;
    }

    MCP41xx_SPI_CS_H();//cs=1; //cs=1;

    mcp_delay(10);//_nop_();
}
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
int32_t fclk_out = 0;
int8_t fclk_change = 0;

int32_t db_out = 0;
int8_t db_change = 0;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM3_Init();
  MX_FSMC_Init();
  MX_TIM6_Init();
  /* USER CODE BEGIN 2 */
    HAL_TIM_Base_Stop_IT(&htim6);
    HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
    TIM3->CCR1 = 1;
    fclk_out = 0;

    char lcd_id[12]; //存放LCD ID字符串
//    HAL_Delay(1000);
    LCD_Init();
    sprintf((char *)lcd_id, "LCD ID:%04X", lcddev.id); //将TFTLCD的ID打印到lcd_id数组。
    LCD_Clear(BLACK);
    POINT_COLOR = RED;
    BACK_COLOR  = WHITE;
    LCD_ShowString(30, 10, 210, 24, 24, "20192332086");
//    LCD_ShowString(30, 40, 210, 24, 24, "20192332077");
    LCD_ShowString(30, 70, 210, 24, 16, "    Frequence: ");
    LCD_ShowString(30, 100, 210, 24, 16, "          Gain: ");
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
//    MCP41xx_writedata(0xff);
  while (1)
  {
      key0_press = HAL_GPIO_ReadPin(KEY0_GPIO_Port,KEY0_Pin);
      if((key0_press != key0_press_past) && key0_press == 1)
      {
          HAL_Delay(500);
          fclk_out -= 1;
          fclk_change = 1;
      }
      key0_press_past = key0_press;

      key1_press = HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin);
      if((key1_press != key1_press_past) && key1_press == 1)
      {
          HAL_Delay(500);
          fclk_out += 1;
          fclk_change = 1;
      }
      key1_press_past = key1_press;

      if(fclk_out > 20)
      {
          fclk_out = 20;
      }
      else if(fclk_out < 0)
      {
          fclk_out = 0;
      }

      if(fclk_change == 1)
      {
          fclk_change = 0;
//          LCD_ShowNum(200, 70, fclk_out,2,16);
          switch (fclk_out)
          {
              case 0:
              {
                  TIM3->PSC = 21 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "20KHz");
                  break;
              }
              case 1:
              {
                  TIM3->PSC = 22 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "19KHz");
                  break;
              }
              case 2:
              {
                  TIM3->PSC = 23 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "18KHz");
                  break;
              }
              case 3:
              {
                  TIM3->PSC = 24 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "17KHz");
                  break;
              }
              case 4:
              {
                  TIM3->PSC = 26 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "16KHz");
                  break;
              }
              case 5:
              {
                  TIM3->PSC = 28 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "15KHz");
                  break;
              }
              case 6:
              {
                  TIM3->PSC = 30 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "14KHz");
                  break;
              }
              case 7:
              {
                  TIM3->PSC = 32 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "13KHz");
                  break;
              }
              case 8:
              {
                  TIM3->PSC = 35 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "12KHz");
                  break;
              }
              case 9:
              {
                  TIM3->PSC = 38 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "11KHz");
                  break;
              }
              case 10:
              {
                  TIM3->PSC = 42 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "10KHz");
                  break;
              }
              case 11:
              {
                  TIM3->PSC = 47 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "9KHz ");
                  break;
              }
              case 12:
              {
                  TIM3->PSC = 52 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "8KHz ");
                  break;
              }
              case 13:
              {
                  TIM3->PSC = 60 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "7KHz");
                  break;
              }
              case 14:
              {
                  TIM3->PSC = 70 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "6KHz ");
                  break;
              }
              case 15:
              {
                  TIM3->PSC = 84 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "5KHz ");
                  break;
              }
              case 16:
              {
                  TIM3->PSC = 105 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "4KHz");
                  break;
              }
              case 17:
              {
                  TIM3->PSC = 140 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "3KHz ");
                  break;
              }
              case 18:
              {
                  TIM3->PSC = 210 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "2KHz ");
                  break;
              }
              case 19:
              {
                  TIM3->PSC = 420 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "1KHz ");
                  break;
              }
              case 20:
              {
                  TIM3->PSC =21000 - 1;
                  HAL_Delay(1);
                  TIM3->ARR = 2 - 1;
                  HAL_Delay(1);
                  TIM3->CCR1 = 1;
                  LCD_ShowString(145, 70, 210, 24, 16, "20Hz ");
                  break;
              }
          }
      }

      key2_press = HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin);
      if ((key2_press != key2_press_past) && key2_press == 1)
      {
          HAL_Delay(500);
          db_change = 1;
          db_out -= 1;
      }
      key2_press_past = key2_press;

      WK_press = HAL_GPIO_ReadPin(WK_UP_GPIO_Port,WK_UP_Pin);
      if((WK_press != WK_press_past) && WK_press == 0)
      {
          HAL_Delay(500);
          db_change = 1;
          db_out += 1;
      }
      WK_press_past = WK_press;

      if(db_out > 11)
      {
          db_out = 11;
      }
      else if(db_out < 0)
      {
          db_out = 0;
      }

      if(db_change == 1)
      {
          db_change = 0;
//          db_out = 25;
          switch (db_out)
          {
              case 0:
              {
                  MCP41xx_writedata(0x00);
                  LCD_ShowString(150, 100, 210, 24, 16, "5dB ");
                  break;
              }
              case 1:
              {
                  MCP41xx_writedata(0x40);
                  LCD_ShowString(150, 100, 210, 24, 16, "10dB");
                  break;
              }
              case 2:
              {
                  MCP41xx_writedata(0xb0);
                  LCD_ShowString(150, 100, 210, 24, 16, "15dB");
                  break;
              }
              case 3:
              {
                  MCP41xx_writedata(0xd9);
                  LCD_ShowString(150, 100, 210, 24, 16, "20dB");
                  break;
              }
              case 4:
              {
                  MCP41xx_writedata(0xed);
                  LCD_ShowString(150, 100, 210, 24, 16, "25dB");
                  break;
              }
              case 5:
              {
                  MCP41xx_writedata(0xf4);
                  LCD_ShowString(150, 100, 210, 24, 16, "30dB");
                  break;
              }
              case 6:
              {
                  MCP41xx_writedata(0xfa);
                  LCD_ShowString(150, 100, 210, 24, 16, "35dB");
                  break;
              }
              case 7:
              {
                  MCP41xx_writedata(0xfd);
                  LCD_ShowString(150, 100, 210, 24, 16, "40dB");
                  break;
              }
              case 8:
              {
                  MCP41xx_writedata(0xfe);
                  LCD_ShowString(150, 100, 210, 24, 16, "45dB");
                  break;
              }
              case 9:
              {
                  MCP41xx_writedata(0xff);
                  LCD_ShowString(150, 100, 210, 24, 16, "50dB");
                  break;
              }
              case 10:
              {
                  MCP41xx_writedata(0xff);
                  LCD_ShowString(150, 100, 210, 24, 16, "55dB");
                  break;
              }
              case 11:
              {
                  MCP41xx_writedata(0xff);
                  LCD_ShowString(150, 100, 210, 24, 16, "60dB");
                  break;
              }
          }
      }
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

